package net.mikoo.seals.api.model.apply;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.api.model.apply.ApplyDetailInfo.Voucher;
import net.mikoo.seals.api.utility.ApiConstants;
import net.mikoo.seals.common.enums.ApplyStatusEnum;
import net.mikoo.seals.common.enums.ApplyTypeEnum;
import net.mikoo.seals.common.enums.VerifyStatusEnum;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.ApplyVerify;
import net.mikoo.seals.model.Upfile;

public class GetApplyDetailResponse extends BaseResponse {

	private static final long serialVersionUID = -1854450753890874573L;

	private ApplyDetailInfo detail;

	public ApplyDetailInfo getDetail() {
		return detail;
	}

	public void setDetail(ApplyDetailInfo detail) {
		this.detail = detail;
	}
	
	public static GetApplyDetailResponse valueOf(Apply apply) {
		GetApplyDetailResponse result = new GetApplyDetailResponse();
		
		if(apply == null ||   apply.getPerson() == null) {
			return result;
		}
		
		ApplyDetailInfo detailInfo = new ApplyDetailInfo();
		detailInfo.setAlias(apply.getSeal().getSealName());
		ApplyTypeEnum applyType = ApplyTypeEnum.getByCode(apply.getApplyType());
		detailInfo.setApplyType(apply.getApplyType());
		detailInfo.setApplyTypeName(applyType.getMessage());
		detailInfo.setCause(apply.getCause());
		detailInfo.setPartyName(apply.getPerson().getName());
		detailInfo.setRemark(apply.getRemark());
		detailInfo.setAddress(apply.getTakeDestination());
		ApplyStatusEnum applyStatus = ApplyStatusEnum.getByCode(apply.getApplyStatus());
		if(applyStatus != null) {
			detailInfo.setStatus(applyStatus.getCode());
			detailInfo.setStatusName(applyStatus.getMessage());
		}
		detailInfo.setTimes(apply.getApplyTimes());
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(apply.getTakeStartTime() != null) {
			detailInfo.setTakeStartTime(sdf.format(apply.getTakeStartTime()));
		}
		if(apply.getTakeEndTime() != null) {
			detailInfo.setTakeEndTime(sdf.format(apply.getTakeEndTime()));
		}
		detailInfo.setApplyTime(sdf.format(apply.getApplyTime()));
		detailInfo.setVerifys(fromVerifyToVerifyRecord(apply.getVerifys()));
		
		if(apply.getSeal() != null) {
			detailInfo.setSealId(apply.getSeal().getSealId());
			detailInfo.setSealName(apply.getSeal().getSealName());
		}
		
		if(apply.getUpfiles() != null) {
			List<Voucher> vouchers = new ArrayList<Voucher>();
			for(Upfile upfile : apply.getUpfiles()) {
				Voucher voucher = new Voucher();
				voucher.setUrl(upfile.getFileUrl());
				if(!upfile.getFileUrl().contains("wx.qlogo.cn")) {
					voucher.setUrl(upfile.getFileUrl());
				}else {
					voucher.setUrl(upfile.getFileUrl());
				}
				
				voucher.setId(upfile.getUpfileId());
				vouchers.add(voucher);
			}
			detailInfo.setVouchers(vouchers);
		}
		
		result.setDetail(detailInfo);
		return result;
	}
	
	private static List<ApplyDetailInfo.VerifyRecored> fromVerifyToVerifyRecord(List<ApplyVerify> verifys){
		
		List<ApplyDetailInfo.VerifyRecored> result = new ArrayList<ApplyDetailInfo.VerifyRecored>();
		if(verifys == null || verifys.isEmpty()) {
			return result;
		}
		
		Collections.sort(verifys, new Comparator<ApplyVerify>() {
			@Override
			public int compare(ApplyVerify o1, ApplyVerify o2) {
				return o1.getCreateTime().compareTo(o2.getCreateTime());
			}
		});
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(ApplyVerify applyVerify : verifys) {
			if(applyVerify == null|| applyVerify.getPerson() == null) {
				continue;
			}
			ApplyDetailInfo.VerifyRecored verifyRecored = new ApplyDetailInfo.VerifyRecored();
			
			
			VerifyStatusEnum verifyStatus = VerifyStatusEnum.getByCode(applyVerify.getVerifyStatus());
			if(verifyStatus != null) {
				verifyRecored.setStatus(verifyStatus.getCode());
				verifyRecored.setStatusName(verifyStatus.getMessage());
			}
			if(applyVerify.getVerifyTime() != null) {
				verifyRecored.setTime(sdf.format(applyVerify.getVerifyTime()));
			}
			
			verifyRecored.setRemark(applyVerify.getRemark());
			verifyRecored.setPartyName(applyVerify.getPerson().getName());
			
			if(StringUtils.isNotBlank(applyVerify.getPerson().getPortrait())) {
				if(!applyVerify.getPerson().getPortrait().contains("wx.qlogo.cn")) {
					verifyRecored.setPortrait(applyVerify.getPerson().getPortrait());
				}else {
					verifyRecored.setPortrait(applyVerify.getPerson().getPortrait());
				}
			}
			
			verifyRecored.setPosition(applyVerify.getPerson().getPosition());
			result.add(verifyRecored);
		}
		
		return result;
	}
}
